package com.demo;

import com.alibaba.fastjson2.JSONObject;
import com.demo.constants.ShopConstant;
import com.demo.interfaces.ICoupon;
import com.demo.interfaces.IFruit;
import com.demo.interfaces.IFruitFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Custom {

    /**
     * 根据购物清单进行结算
     * @param jsonObject 购物清单
     * @return int 商品总价
     */
    public static int settle(JSONObject jsonObject) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        int total = 0;
        JSONObject json;
        String shopDiscountType = null;
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            String name = entry.getKey();
            //根据水果类型获取对应水果工厂实例
            IFruitFactory fruitBean = getFruitBean(name);
            if (ShopConstant.SHOP_DISCOUNT_TYPE.equals(name)) {
                shopDiscountType = (String) entry.getValue();
                continue;
            }else{
                json = (JSONObject) entry.getValue();
            }
            int unitPrice = (int) json.get(ShopConstant.UNIT_PRICE);
            int weight = (int) json.get(ShopConstant.WEIGHT);
            int discount = (int) json.get(ShopConstant.FRUIT_DISCOUNT);
            int singleTotal = 0;
            if(fruitBean != null){
                IFruit fruit = fruitBean.getFruit();
                singleTotal = fruit.getTotalPrice(unitPrice, weight, discount);
            }
            total = total + singleTotal;
        }
        //根据商店的优惠券类型获取对应优惠券实例
        ICoupon couponBean = getCouponBean(shopDiscountType);
        if(couponBean != null){
            total = couponBean.fullReduction(total);
        }
        return total;
    }
    /**
     * 根据水果类型获取对应水果工厂实例
     * @param fruitType
     * @return IFruitFactory
     */
    private static IFruitFactory getFruitBean(String fruitType) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Properties fruitPros =  new Properties();
        InputStream ins = Custom.class.getResourceAsStream("properties/fruitBean.properties");
        fruitPros.load(ins);
        Set<String> fruitSet = fruitPros.stringPropertyNames();
        Iterator<String> fruitIt = fruitSet.iterator();
        IFruitFactory fruitBean = null;
        while (fruitIt.hasNext()){
            String name = fruitIt.next();
            String info = fruitPros.getProperty(name);
            if(name.equals(fruitType)) {
                fruitBean = (IFruitFactory) Class.forName(info).newInstance();
                break;
            }
        }
        return fruitBean;
    }

    /**
     * 根据商店的优惠券类型获取对应优惠券实例
     * @param shopDiscountType
     * @return ICoupon
     */
    private static ICoupon getCouponBean(String shopDiscountType) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Properties couponPros =  new Properties();
        InputStream ins = Custom.class.getResourceAsStream("properties/couponBean.properties");
        couponPros.load(ins);
        Set<String> couponSet = couponPros.stringPropertyNames();
        Iterator<String> couponIt = couponSet.iterator();
        ICoupon couponBean = null;
        while (couponIt.hasNext()){
            String name = couponIt.next();
            String info = couponPros.getProperty(name);
            if(name.equals(shopDiscountType)){
              couponBean = (ICoupon) Class.forName(info).newInstance();
              break;
            }
        }
        return couponBean;
    }
}
